package com.mall.search.mallsearch.controller;

import com.mall.search.mallsearch.api.ResponseVo;
import com.mall.search.mallsearch.service.IOperationFacade;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@Api(tags = "downLoadController", description = "模型文件下载接口")
@RequestMapping("/downLoad")
public class DownLoadController {
    private static final Logger LOG = LoggerFactory.getLogger(DownLoadController.class);

    @Autowired
    private IOperationFacade operationFacade;

    @ApiOperation(value = "excel导入模型数据下载接口", notes = "excel导入格式必须按照指定格式")
    @RequestMapping(method = RequestMethod.POST, value = "/addCenterPointFromExcel")
    public ResponseVo addCenterPointFromExcel(@RequestParam("file") MultipartFile file) {
        ResponseVo responseVo = new ResponseVo();
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        if (!file.isEmpty()) {
            try {
                LOG.info("add centerPoint From Excel,file name {},suffix {}", file.getOriginalFilename(), suffix);
                String msg = operationFacade.addCenterPointFromExcel(file.getInputStream());
                responseVo.setCode(1);
                responseVo.setMsg(msg);
                responseVo.setSuccess(true);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            LOG.error("message" + "Failed to upload " + file.getOriginalFilename() + " because it was empty");
        }
        return responseVo;
    }
}
